﻿// -----------------------------------------------------------------------
// <copyright file="ImageFormatElementCollection.cs" company="EPAM Systems">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Configuration.ConfigElements
{
	#region Usings

	using System.Collections.Generic;
	using System.Configuration;
	using System.Text;

	#endregion

	/// <summary>
	/// TODO: Update summary.
	/// </summary>
	[ConfigurationCollection(typeof(ImageFormatElement))]
	public class ImageFormatElementCollection : ConfigurationElementCollection
	{
		#region Indexers

		/// <summary>
		/// Gets configuration element by specified index.
		/// </summary>
		/// <param name="index">The specified index.</param>
		/// <returns>The admin element</returns>
		public ImageFormatElement this[int index]
		{
			get
			{
				return (ImageFormatElement)BaseGet(index);
			}
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Returns a string that represents the current object.
		/// </summary>
		/// <returns>A string that represents the current object.</returns>
		public override string ToString()
		{
			List<string> formats = new List<string>();

			for (int index = 0; index < this.Count; index++)
			{
				if (!formats.Contains(this[index].Name))
				{
					formats.Add(this[index].Name);
				}
			}

			StringBuilder formatsString = new StringBuilder();

			for (int index = 0; index < formats.Count; index++)
			{
				formatsString.Append(formats[index]);

				if (index != formats.Count - 1)
				{
					formatsString.Append(", ");
				}

				if (index == 5)
				{
					formatsString.AppendLine();
				}
			}

			return formatsString.ToString();
		}

		#endregion

		#region Protected Methods

		/// <summary>
		/// Creates new configuration element.
		/// </summary>
		/// <returns>Configuration element.</returns>
		protected override ConfigurationElement CreateNewElement()
		{
			return new ImageFormatElement();
		}

		/// <summary>
		/// Gets the configuration element key.
		/// </summary>
		/// <param name="element">The configuration element.</param>
		/// <returns>The key.</returns>
		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((ImageFormatElement)element).MimeType;
		}

		#endregion
	}
}
